def apply_edge_map_to_path(path,edge_map):
    new_path = []    
    
    while len(path) > 0:       
        
        e = path.pop(0)        
        if edge_map.has_key(e):
            new_path += edge_map[e]
        else:
            new_path.append(e)
    return new_path

def apply_vertex_map_to_path(path,vertex_map):
    new_path = []
    
    while len(path) > 0:
        e = path.pop(0)         
        new_path.append((vertex_map[e[0]],vertex_map[e[1]],e[2]))
    return new_path

def apply_n_segment_derivation_decode_to_path(path):
    new_path = []
    
    tt = type((1,2,3))
    
    while len(path) > 0:       
        
        e = path.pop(0)
        bool = 1
        
        #if (type(e[0]) == tt) & (len(e[0]) == 4) & (type(e[1]) == tt) & (len(e[1]) == 4):
        
        try:            
            if (len(e[0]) == 4) & (len(e[1]) == 4):
                if e[0][3] == 'S': #case S->E
                    new_path.append((e[0][0],e[0][1],e[0][2]))
                #else nothing! case E->S
                
                bool = 0
        except TypeError:
            pass
        
        
        try:        
            if (len(e[0]) == 4) & bool:                
                new_path.append((e[0][0],e[0][1],e[0][2]))
                bool = 0
        except TypeError:
            pass
        try:
            if (len(e[1]) == 4) & bool:
                new_path.append((e[1][0],e[1][1],e[1][2]))
                bool = 0
        except TypeError:
            pass
        
        if bool: new_path.append(e);
                        
    return new_path
    

def identify_n_segments(old, new):
    
    i = 0;
    map = {}
    
    while i < len(new):
        path = new[i]
        to = []
        
        j = 0;
        k = 1;
        
        while j+k <= len(path):
            testing = path[j:(j+k)]
            
            try:
                m = old.index(testing)
                to.append(m)
                j = j+k
                k = 1
            except ValueError:
                k = k+1
                pass #not present
            
        if j+k > len(path) & k > 1:
            print 'segment identification problem'
             
        map[i] = to        
        i = i+1
    
    return map